package com.zhenmaitang.clinic_sys.common;

/**
 * 统一响应状态码枚举类
 * 规范API接口返回的状态码和消息
 */
public enum ResultCode implements CodeEnum{
    
    // 成功状态码
    SUCCESS(2000, "操作成功"),
    
    // 客户端错误状态码
    BAD_REQUEST(4000, "请求参数错误"),
    UNAUTHORIZED(4001, "未授权访问"),
    FORBIDDEN(4003, "权限不足"),
    NOT_FOUND(4004, "资源不存在"),
    CONFLICT(4009, "资源冲突"),
    UNPROCESSABLE_ENTITY(4022, "业务逻辑错误"),
    
    // 服务器错误状态码
    INTERNAL_SERVER_ERROR(5000, "服务器内部错误"),
    SERVICE_UNAVAILABLE(5003, "服务暂不可用");
    
    private final int code;
    private final String message;
    
    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public int getCode() {
        return code;
    }
    
    @Override
    public String getMessage() {
        return message;
    }
    
    public static ResultCode valueOf(int code) {
        for (ResultCode resultCode : values()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }
    
    /**
     * 获取对应HTTP状态码的描述
     */
    public String getDescription() {
        switch (this) {
            case SUCCESS:
                return "请求处理成功";
            case BAD_REQUEST:
                return "客户端请求参数有误";
            case UNAUTHORIZED:
                return "需要身份认证";
            case FORBIDDEN:
                return "权限不足，拒绝访问";
            case NOT_FOUND:
                return "请求的资源不存在";
            case CONFLICT:
                return "资源已存在或发生冲突";
            case UNPROCESSABLE_ENTITY:
                return "请求格式正确但语义错误";
            case INTERNAL_SERVER_ERROR:
                return "服务器内部发生错误";
            case SERVICE_UNAVAILABLE:
                return "服务器暂时无法处理请求";
            default:
                return "未知错误";
        }
    }
}